import userApi from "../../api/chaoxing/userApi/index"
import { throttle } from "../../api/HOF/throttle"
import dbUserApi from "../../api/wechatCloud/dbUserApi/index"
import showToast from "../../utils/toast"

export const submiConfigs = throttle((courseConfigs: ICourseConfig[]) => {
  if (courseConfigs.length > 10) {
    showToast({ title: "最多配置10个", icon: "error" })
    return;
  }
  console.log(courseConfigs)
  dbUserApi.setOrUpdateConfigByUsername({
    _id: userApi.getCurUser(),
    corn_trigger: courseConfigs,
  }).then(() => {
    showToast({ title: '配置提交成功', icon: "success" })
  })
})

export const refreshConfigs = throttle((courseTitleRange: string[], that:any) => {
  dbUserApi.getConfigByUsername(userApi.getCurUser()).then(
    res => {
      console.log(res)
      res.data[0]?.corn_trigger?.map(
        couseConfig => {
          couseConfig.courseIdx = '' + courseTitleRange.findIndex(courseTitle => courseTitle == couseConfig.course.title)
          return couseConfig
        }
      )
      console.log(res)
      that.setData({ courseConfigs: res.data[0]?.corn_trigger || []})
      showToast({title:'同步配置成功'})
    }
  )
})